package com.recsmile.rpc.consumer.net;

import com.recsmile.rpc.consumer.net.codec.MessageRecvHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;

import java.util.Map;

/**
 * Created by Administrator on 2016/8/7.
 */
public class MessageRecvChannelInitializer extends ChannelInitializer<Channel> {
    private Map<String, Object> serviceMap;

    public MessageRecvChannelInitializer(Map<String, Object> serviceMap) {
        this.serviceMap = serviceMap;
    }

    protected void initChannel(Channel ch) throws Exception {
        ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4),
                new LengthFieldPrepender(4), new ObjectEncoder(),
                new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())),
                new MessageRecvHandler(serviceMap));
    }
}
